home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_se / e_inspect.e < prev    next >
Text File  |  2000-03-25  |  9KB  |  307 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class E_INSPECT
  17.    --
  18.    -- The Eiffel inspect instruction.
  19.    --
  20.  
  21. inherit INSTRUCTION;
  22.  
  23. creation make
  24.  
  25. feature
  26.  
  27.    start_position: POSITION;
  28.          -- Of keyword `inspect'.
  29.  
  30.    expression: EXPRESSION;
  31.          -- Heading expression after keyword `inspect'.
  32.  
  33.    when_list: WHEN_LIST;
  34.          -- List of when clauses.
  35.  
  36.    else_position: POSITION;
  37.          -- Of the keyword `else' if any.
  38.  
  39.    else_compound: COMPOUND;
  40.          -- Else compound if any.
  41.  
  42. feature {NONE}
  43.  
  44.    current_type: TYPE;
  45.  
  46. feature {NONE}
  47.  
  48.    make(sp: like start_position; exp: like expression) is
  49.       require
  50.          not sp.is_unknown;
  51.          exp /= Void;
  52.       do
  53.          start_position := sp;
  54.          expression := exp;
  55.       ensure
  56.          start_position = sp;
  57.          expression = exp;
  58.       end;
  59.  
  60. feature
  61.  
  62.    is_pre_computable: BOOLEAN is false;
  63.  
  64.    end_mark_comment: BOOLEAN is true;
  65.  
  66. feature
  67.  
  68.    afd_check is
  69.       do
  70.          expression.afd_check;
  71.          if when_list /= Void then
  72.             when_list.afd_check;
  73.          end;
  74.          if else_compound /= Void then
  75.             else_compound.afd_check;
  76.          end;
  77.       end;
  78.  
  79.    includes(v: INTEGER): BOOLEAN is
  80.       -- True if a when clause includes `v'.
  81.       do
  82.          Result := when_list.includes_integer(v);
  83.       end;
  84.  
  85.    collect_c_tmp is
  86.       do
  87.       end;
  88.  
  89.    compile_to_c is
  90.       local
  91.          no_check: BOOLEAN;
  92.          debug_check: BOOLEAN;
  93.       do
  94.          no_check := run_control.no_check;
  95.          debug_check := run_control.debug_check;
  96.          cpp.inspect_incr;
  97.          cpp.put_string("{int ");
  98.          cpp.put_inspect;
  99.          cpp.put_character('=');
  100.          if debug_check then
  101.             cpp.put_character('(');
  102.             cpp.se_trace_exp(expression.start_position);
  103.             cpp.put_character(',');
  104.          end;
  105.          expression.compile_to_c;
  106.          if debug_check then
  107.             cpp.put_character(')');
  108.          end;
  109.          cpp.put_string(fz_00);
  110.          if when_list = Void then
  111.             if else_position.is_unknown then
  112.                if no_check then
  113.                   exceptions_handler.incorrect_inspect_value(start_position);
  114.                end;
  115.             elseif else_compound /= Void then
  116.                else_compound.compile_to_c;
  117.             end;
  118.          else
  119.             when_list.compile_to_c(else_position);
  120.             if else_position.is_unknown then
  121.                if no_check then
  122.                   cpp.put_character(' ');
  123.                   cpp.put_string(fz_else);
  124.                   cpp.put_character('{');
  125.                   exceptions_handler.incorrect_inspect_value(start_position);
  126.                   cpp.put_character('}');
  127.                end;
  128.             elseif else_compound /= Void then
  129.                cpp.put_character(' ');
  130.                cpp.put_string(fz_else);
  131.                cpp.put_character('{');
  132.                else_compound.compile_to_c;
  133.                cpp.put_character('}');
  134.             end;
  135.          end;
  136.          cpp.put_string(fz_12);
  137.          cpp.inspect_decr;
  138.       end;
  139.  
  140.    compile_to_jvm is
  141.       do
  142.          expression.compile_to_jvm;
  143.          if when_list /= Void then
  144.             when_list.compile_to_jvm(else_position);
  145.          end;
  146.          if else_compound /= Void then
  147.             else_compound.compile_to_jvm;
  148.          elseif else_position.is_unknown then
  149.             if run_control.no_check then
  150.                code_attribute.runtime_error_inspect(expression);
  151.             end;
  152.          end;
  153.          if when_list /= Void then
  154.             when_list.compile_to_jvm_resolve_branch;
  155.          end;
  156.          code_attribute.opcode_pop;
  157.       end;
  158.  
  159.    use_current: BOOLEAN is
  160.       do
  161.          Result := Result or else expression.use_current;
  162.          if when_list /= Void then
  163.             Result := Result or else when_list.use_current;
  164.          end;
  165.          if else_compound /= Void then
  166.             Result := Result or else else_compound.use_current;
  167.          end;
  168.       end;
  169.  
  170.    stupid_switch(r: ARRAY[RUN_CLASS]): BOOLEAN is
  171.       do
  172.          Result := expression.stupid_switch(r) and then
  173.                    (when_list = Void or else when_list.stupid_switch(r)) and then
  174.                    (else_compound = Void or else else_compound.stupid_switch(r));
  175.       end;
  176.  
  177.    add_when(e_when: E_WHEN) is
  178.       require
  179.          e_when /= Void
  180.       do
  181.          if when_list = Void then
  182.             !!when_list.make(e_when);
  183.          else
  184.             when_list.add_last(e_when);
  185.          end;
  186.       end;
  187.  
  188.    set_else_compound(sp: like else_position; ec: like else_compound) is
  189.       do
  190.          else_position := sp;
  191.          else_compound := ec;
  192.       end;
  193.  
  194.    to_runnable(ct: TYPE): like Current is
  195.       local
  196.          e: like expression;
  197.          te: TYPE;
  198.          wl: WHEN_LIST;
  199.       do
  200.          if current_type = Void then
  201.             current_type := ct;
  202.             e := expression.to_runnable(ct);
  203.             if nb_errors = 0 then
  204.                expression := e;
  205.                te := e.result_type.run_type;
  206.                --                  ********
  207.                --                  VIRABLE
  208.             end;
  209.             if nb_errors = 0 then
  210.                if te.is_character then
  211.                   if when_list /= Void then
  212.                      when_list := when_list.to_runnable_character(Current);
  213.                      if when_list = Void then
  214.                         error(start_position,em1);
  215.                      end;
  216.                   end;
  217.                elseif te.is_integer then
  218.                   if when_list /= Void then
  219.                      when_list := when_list.to_runnable_integer(Current);
  220.                      if when_list = Void then
  221.                         error(start_position,em1);
  222.                      end;
  223.                   end;
  224.                else
  225.                   eh.append("Expression must be INTEGER or CHARACTER.");
  226.                   eh.add_type(te," is not allowed.");
  227.                   eh.add_position(start_position);
  228.                   eh.print_as_error;
  229.                end;
  230.             end;
  231.             if else_compound /= Void then
  232.                else_compound := else_compound.to_runnable(ct);
  233.             end;
  234.             Result := Current
  235.          else
  236.             Result := twin;
  237.             !!wl.from_when_list(when_list);
  238.             Result.set_when_list(wl);
  239.             Result.clear_current_type;
  240.             Result := Result.to_runnable(ct);
  241.          end;
  242.       end;
  243.  
  244. feature
  245.  
  246.    pretty_print is
  247.       do
  248.          fmt.keyword(fz_inspect);
  249.          fmt.level_incr;
  250.          if not fmt.zen_mode then
  251.             fmt.indent;
  252.          end;
  253.          fmt.set_semi_colon_flag(false);
  254.          expression.pretty_print;
  255.          fmt.level_decr;
  256.          fmt.indent;
  257.          if when_list /= Void then
  258.             when_list.pretty_print;
  259.          end;
  260.          if else_compound = Void then
  261.             if not else_position.is_unknown then
  262.                fmt.indent;
  263.                fmt.keyword(fz_else);
  264.             end;
  265.          else
  266.             fmt.indent;
  267.             fmt.keyword(fz_else);
  268.             fmt.level_incr;
  269.             else_compound.pretty_print;
  270.             fmt.level_decr;
  271.          end;
  272.          fmt.indent;
  273.          fmt.keyword("end;");
  274.          if fmt.print_end_inspect then
  275.             fmt.put_end(fz_inspect);
  276.          end;
  277.       end;
  278.  
  279. feature {E_INSPECT}
  280.  
  281.    set_when_list(wl: like when_list) is
  282.       do
  283.          when_list := wl;
  284.       ensure
  285.          when_list = wl;
  286.       end;
  287.  
  288. feature {NONE}
  289.  
  290.    em1: STRING is "Bad inspect.";
  291.  
  292. feature {E_INSPECT}
  293.  
  294.    clear_current_type is
  295.       do
  296.          current_type := Void;
  297.       ensure
  298.          current_type = Void
  299.       end;
  300.  
  301. invariant
  302.  
  303.    expression /= Void;
  304.  
  305. end -- E_INSPECT
  306.  
  307.